import {handler, view, ViewGroup} from "adajs";
import SettinghelpService from "./state.js";
import Bar from "../../../../controls/bar";
import Form from "./../../../../controls/form";
import User from "../../../../lib/user";
import Loading from "../../../../controls/loading";
import Toast from "../../../../controls/toast";

@view({
    className: "settinghelp",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: SettinghelpService
    }
})
class SettingPasswordPage extends ViewGroup {
    tags() {
        return {
            bar: Bar,
            form: Form
        }
    }

    @handler("reset")
    reset(e) {
        let form = e.target;
        form.check().then(a => {
            if (a) {
                form.getValue().then(a => {
                    if (a.confpwd === a.newpwd) {
                        a.userName = User.getUserInfo().u;
                        this.addChild(Loading).then(loading => {
                            this.context.request.post('/api/admin/user/updatePassword', a).then(a => {
                                if (a.code == 0) {
                                    loading.showError(a.msg);
                                    loading.close();
                                } else {
                                    loading.showSuccess('重置成功');
                                    loading.close().then(() => {
                                        User.clean();
                                        this.dispatchEvent('gotoPage', User.getUserMap().loginPath);
                                    });
                                }
                            });
                        });
                    } else {
                        this.addChild(Toast, {
                            parameter: {
                                content: "两次密码不同"
                            }
                        })
                    }
                })
            }
        });
    }
}

export default SettingPasswordPage;